# encoding: utf-8
class WorkpackagesController < ApplicationController
  before_filter :is_member_of_project?
  before_filter :authenticate_user!
  before_filter :get_project

  def index
    @workpackages = Workpackage.find_all_by_project_id(params[:project_id])

    if ( session["openall"] == "1" )
      @workpackages.each do |workpackage|
        session[workpackage.id] = "#{workpackage.id}"
      end
    end
    if ( session["closeall"] == "1" )
      @workpackages.each do |workpackage|
        session[workpackage.id] = "0"
      end
    end

    respond_to do |format|
      format.html
      format.xml { render :xml => @workpackages }
    end
  end

  def open
    session["openall"] = "0"
    session["closeall"] = "0"
    session[params[:id]] = params[:id]

    respond_to do |format|
      format.html { redirect_to :action => "index" }
      format.xml { render :xml => @workpackage }
    end
  end

  def close
    session["openall"] = "0"
    session["closeall"] = "0"
    session[params[:id]] = 0

    respond_to do |format|
      format.html { redirect_to :action => "index" }
      format.xml { render :xml => @workpackage }
    end
  end

  def openall
    session["openall"] = "1"
    session["closeall"] = "0"

    respond_to do |format|
      format.html { redirect_to :action => "index" }
      format.xml { render :xml => @workpackage }
    end
  end

  def closeall
    session["closeall"] = "1"
    session["openall"] = "0"
    
    respond_to do |format|
      format.html { redirect_to :action => "index" }
      format.xml { render :xml => @workpackage }
    end
  end

  def show
    @workpackage = Workpackage.find(params[:id])

    respond_to do |format|
      format.html
      format.xml { render :xml => @workpackage }
    end
  end

  def new
    if is_project_manager?
      @workpackage = Workpackage.new
      @workpackage.tasks.build

      respond_to do |format|
        format.html
        format.xml { render :xml => @workpackage }
      end
    else
      flash[:error] = t :workpackage_no_right_create, :scope => [:flash, :error]
      respond_to do |format|
        format.html { redirect_to :action => "index" }
        format.xml { render :xml => @workpackage }
      end
    end
  end

  def create
    @workpackage = Workpackage.new(params[:workpackage])
    @workpackage.project_id = params[:project_id]

    respond_to do |format|
      if @workpackage.save
        set_progress
        flash[:notice] = t :workpackage_create_success, :scope => [:flash, :notice]
        format.html { redirect_to(project_workpackage_path(params[:project_id], @workpackage)) }
        format.xml  { render :xml => @workpackage, :status => :created, :location => @workpackage }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @workpackage.errors, :status => :unprocessable_entity }
      end
    end
  end

  def edit
    if is_project_manager?
      @workpackage = Workpackage.find(params[:id])
    else
      flash[:error] = t :workpackage_no_right_edit, :scope => [:flash, :error]
      respond_to do |format|
        format.html { redirect_to :action => "index" }
        format.xml { render :xml => @workpackage }
      end
    end
  end

  def update
    @workpackage = Workpackage.find(params[:id])

    respond_to do |format|
      if @workpackage.update_attributes(params[:workpackage])
        set_progress
        flash[:notice] = t :workpackage_edit_success, :scope => [:flash, :notice]
        format.html { redirect_to(project_workpackage_path(params[:project_id], @workpackage)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @workpackage.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    if is_project_manager?
      @workpackage = Workpackage.find(params[:id])
      set_progress

      if @workpackage.destroy
        respond_to do |format|
          flash[:notice] = t :workpackage_delete_success, :scope => [:flash, :notice]
          format.html { redirect_to ( project_path(params[:project_id]) ) }
          format.xml  { head :ok }
        end
      else
        set_progress
        flash.now[:error] = t :workpackage_delete_error, :scope => [:flash, :error]
        respond_to do |format|
          format.html { render :action => "show" }
          format.xml  { render :xml => @workpackage.errors, :status => :unprocessable_entity }
        end
      end
    else
      flash[:error] = t :workpackage_no_right_delete, :scope => [:flash, :error]
      respond_to do |format|
        format.html { redirect_to :action => "index" }
        format.xml { render :xml => @workpackage }
      end
    end
  end

end
